package com.klm.datakeeper.filemonitoring;

import java.io.File;
import java.io.IOException;

import android.os.FileObserver;
import android.util.Log;

import com.dropbox.sync.android.DbxException;
import com.dropbox.sync.android.DbxFile;
import com.dropbox.sync.android.DbxFileSystem;
import com.dropbox.sync.android.DbxPath;
import com.dropbox.sync.android.DbxPath.InvalidPathException;

public class FilePathObserver extends FileObserver {
	
	private static final String TAG = "FILEOBSERVER";
	private String rootPath;
	private DbxFileSystem dbxFileSystem;
	private FileModificationService fileModificationService;
	
	public FilePathObserver(String path, DbxFileSystem dbxFileSystem, FileModificationService fileModificationService) {
		super(path, FileObserver.ALL_EVENTS);
		this.fileModificationService = fileModificationService;
		this.dbxFileSystem = dbxFileSystem;
		if (!path.endsWith(File.separator)) {
			path += File.separator;
		}
		File file = new File(path);
		createFilesInDbxIfNotExists(file);
		Log.d(TAG, "Added path for monitoring:" + path);
		
		rootPath = path;
	}
	
	private void createFilesInDbxIfNotExists(File file){
		try {
			DbxPath dbxPath = new DbxPath(file.getAbsolutePath());
			if(file.isDirectory()){
				if(!dbxFileSystem.exists(dbxPath))
					dbxFileSystem.createFolder(dbxPath);
				for(File subFile : file.listFiles())
					createFilesInDbxIfNotExists(subFile);
			}else {
				if(!dbxFileSystem.exists(dbxPath)){
					DbxFile dbxFile = dbxFileSystem.create(dbxPath);
					dbxFile.writeFromExistingFile(file, false);
					dbxFile.close();
				}
			}
		} catch (InvalidPathException e) {
			e.printStackTrace();
		} catch (DbxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void onEvent(int event, String path) {
		if (path == null) 
			return;

		if ((FileObserver.CREATE & event) != 0) {
			createFileBackup(rootPath + path);
			Log.d(TAG, "CREATE:" + rootPath + path);
		}
		
		if ((FileObserver.DELETE & event) != 0) {
			deleteFileBackup(rootPath + path);
			Log.d(TAG, "CREATE:" + rootPath + path);
		}
	}
	
	private void createFileBackup(String path){
		try {
			File file = new File(path);
			if(file.isDirectory()){
				dbxFileSystem.createFolder(new DbxPath(path));
				FilePathObserver filePathObserver = fileModificationService.createFileObserver(file);
				filePathObserver.startWatching();
			}
			else{
				
				DbxFile dbxFile = dbxFileSystem.create(new DbxPath(path));
				dbxFile.writeFromExistingFile(file, false);
				dbxFile.close();
			}
		} catch (InvalidPathException e) {
			e.printStackTrace();
		} catch (DbxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void deleteFileBackup(String path){
		try {
			DbxPath dbxPath = new DbxPath(path);
			if(dbxFileSystem.exists(dbxPath))
				dbxFileSystem.delete(dbxPath);
		} catch (InvalidPathException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DbxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void close() {
		super.finalize();
	}
}